"""
Case Type   : cm_ctl
Case Name   : 在不同AZ的CM集群中,执行cm_ctl pause/resume,执行cm命令是否可用
Create At   : 2023/03/24
Owner       : opentestcase012
Description :
    1: 所有节点下查看暂停标志文件;
    2: 执行cm_ctl pause;
    3: 再次查看查看暂停标志文件;
    4: 查看cm_agent下的cm_agent-xxx-current.log日志;
    5: 查看集群状态;
    6: 停止集群;
    7: 查看集群状态;
    8: 启动集群;
    9: 查看集群状态;
    10: 在各节点指定主备机;
    11: 查看集群状态;
    12: 恢复集群状态;
    13: 执行cm_ctl resume;
    14: 再次查看查看暂停标志文件;
Expect      :
    1: 无暂停标志文件
    2: 执行成功
    3: 生成暂停标志文件
    4: agent日志中提示The cluster has been paused
    5: 查看状态成功,集群状态中有暂停状态pausing         : Yes
    6: 执行成功
    7: 集群停止,不显示集群状态Cluster State
    8: 启动超时失败
    9: 集群状态异常,dn为pending状态
    10: 指定成功
    11: 集群状态正常
    12: 恢复成功
    13: 执行成功
    14: 暂停标志文件被删除
History     :
"""
import os
import unittest
import time

from yat.test import Node
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro

primary_sh = CommonSH('PrimaryDbUser')


# @unittest.skipIf(2 != primary_sh.get_node_num(), '非1+2环境不执行')
class DefinedResource(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.common = Common()
        self.constant = Constant()
        self.pri_user = Node('PrimaryDbUser')
        self.sta1_sh = CommonSH('Standby1DbUser')
        self.sta2_sh = CommonSH('Standby2DbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.pause_file = os.path.join(macro.GAUSSHOME_BIN_PATH,
                                       'cluster_manual_pause')

    def test_cm(self):
        text = '----step1:所有节点下查看暂停标志文件; expect:无暂停标志文件----'
        self.log.info(text)
        res = self.primary_sh.exec_gs_ssh(f'ls {self.pause_file}')
        self.assertEqual(3, res.count('No such file or directory'),
                         "执行失败:" + text)

        text = '----查看cm_agent日志中pause日志; expect:成功'
        self.log.info(text)
        grep_cmd = f"cd {macro.CMA_LOG_PATH};" \
            f"grep -c 'The cluster has been paused' cm_agent-*-current.log"
        log_count_old = self.common.get_sh_result(self.pri_user, grep_cmd)

        text = '----step2:执行cm_ctl pause; expect:执行成功----'
        self.log.info(text)
        cm_ctl_res = self.primary_sh.exec_cm_ctl('pause')
        self.assertIn(self.constant.cm_pause_success_msg, cm_ctl_res,
                      '执行失败:' + text)

        text = '----step3:再次查看暂停标志文件; expect:生成暂停标志文件----'
        self.log.info(text)
        res = self.primary_sh.exec_gs_ssh(f'ls {self.pause_file}')
        self.assertEqual(3, res.count(self.pause_file), "执行失败:" + text)

        text = '----step4:查看cm_agent下的cm_agent-xxx-current.log日志; ' \
               'expect:agent日志中提示The cluster has been paused----'
        self.log.info(text)
        grep_cmd = f"cd {macro.CMA_LOG_PATH};" \
            f"grep -c 'The cluster has been paused' cm_agent-*-current.log"
        log_count_new = self.common.get_sh_result(self.pri_user, grep_cmd)
        self.assertTrue(int(log_count_new) - int(log_count_old) > 1,
                        "执行失败:" + text)

        text = '----step5:查看集群状态; expect:查看状态成功,集群状态中有暂停状态----'
        self.log.info(text)
        cm_ctl_res = self.primary_sh.exec_cm_ctl('query', '-Civ')
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, cm_ctl_res,
                      '执行失败:' + text)
        self.assertIn('pausing         : Yes', cm_ctl_res, '执行失败:' + text)

        text = '----step6:停止集群; expect:执行成功----'
        self.log.info(text)
        cm_ctl_res = self.primary_sh.exec_cm_ctl('stop')
        self.assertIn(self.constant.cm_stop_success_msg, cm_ctl_res,
                      '执行失败:' + text)

        time.sleep(10)
        text = '----step7:查看集群状态; expect:集群停止,不显示集群状态Cluster State----'
        self.log.info(text)
        cm_ctl_res = self.primary_sh.exec_cm_ctl('query', '-Civ')
        self.assertNotIn('Cluster State', cm_ctl_res, '执行失败:' + text)

        text = '----step8:启动集群; expect:启动超时失败----'
        self.log.info(text)
        cm_ctl_res = self.primary_sh.exec_cm_ctl('start')
        self.assertIn('cm_ctl: start cluster failed in (601)s', cm_ctl_res,
                      '执行失败:' + text)

        time.sleep(10)
        text = '----step9:查看集群状态; expect:集群状态异常,dn为pending状态----'
        self.log.info(text)
        cm_ctl_res = self.primary_sh.exec_cm_ctl('query', '-Civ')
        self.assertEqual(3, cm_ctl_res.count('Pending'), '执行失败:' + text)

        text = '----step10:在各节点指定主备机; expect:指定成功----'
        self.log.info(text)
        gs_ctl_res = self.primary_sh.execute_gsctl(
            'notify', self.constant.notify_success_msg, param='-M primary')
        self.assertTrue(gs_ctl_res, '执行失败:' + text)
        gs_ctl_res = self.sta1_sh.execute_gsctl(
            'notify', self.constant.notify_success_msg, param='-M standby')
        self.assertTrue(gs_ctl_res, '执行失败:' + text)
        gs_ctl_res = self.sta2_sh.execute_gsctl(
            'notify', self.constant.notify_success_msg, param='-M standby')
        self.assertTrue(gs_ctl_res, '执行失败:' + text)

        time.sleep(10)
        text = '----step11:查看集群状态; expect:集群状态正常----'
        self.log.info(text)
        cm_ctl_res = self.primary_sh.exec_cm_ctl('query', '-Civ')
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, cm_ctl_res,
                      '执行失败:' + text)

        text = '----step12:恢复集群状态; expect:恢复成功----'
        self.log.info(text)
        cm_ctl_res = self.primary_sh.exec_cm_ctl('resume')
        self.assertIn(self.constant.cm_resume_success_msg, cm_ctl_res,
                      '执行失败:' + text)
        cm_ctl_res = self.primary_sh.exec_cm_ctl('start')
        self.assertIn(self.constant.cm_start_success_msg, cm_ctl_res,
                      '执行失败:' + text)

        text = '----step13:执行cm_ctl resume; expect:执行成功----'
        self.log.info(text)
        cm_ctl_res = self.primary_sh.exec_cm_ctl('resume')
        self.assertIn(self.constant.cm_resume_success_msg, cm_ctl_res,
                      '执行失败:' + text)

        text = '----step14:再次查看暂停标志文件; expect:暂停标志文件被删除----'
        self.log.info(text)
        res = self.primary_sh.exec_gs_ssh(f'ls {self.pause_file}')
        self.assertEqual(3, res.count('No such file or directory'),
                         "执行失败:" + text)

    def tearDown(self):
        self.log.info('-----清理环境-----')
        text = '----step15:恢复集群状态; expect:执行成功----'
        self.log.info(text)
        cm_ctl_res1 = self.primary_sh.exec_cm_ctl('resume')
        cm_ctl_res2 = self.primary_sh.exec_cm_ctl('start')
        cm_ctl_res3 = self.primary_sh.exec_cm_ctl('switchover', '-a')
        time.sleep(10)
        cm_ctl_res4 = self.primary_sh.exec_cm_ctl('query', '-Civ')
        self.assertIn(self.constant.cm_resume_success_msg, cm_ctl_res1,
                      '执行失败:' + text)
        self.assertIn(self.constant.cm_start_success_msg, cm_ctl_res2,
                      '执行失败:' + text)
        self.assertIn(self.constant.cm_switchover_success_msg, cm_ctl_res3,
                      '执行失败:' + text)
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, cm_ctl_res4,
                      "执行失败:" + text)
        self.log.info(f'----{os.path.basename(__file__)} finish----')
